package dao;

import utils.connect.ConnectSqlDaoImpl;
import utils.string.sql.T_User;
import vo.User;

import java.sql.*;
import java.util.HashMap;

public class UserDaoImpl implements UserDao {
    ConnectSqlDaoImpl connectSqlDao = ConnectSqlDaoImpl.getInStance();
    @Override
    public boolean deleteByID(int id) {
        Connection connection =null;
        try {
            connectSqlDao.initConnect();
            connection = connectSqlDao.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
        PreparedStatement preparedStatement = null;
        try {
            String sql = "delete from "+ T_User.TABLE_NAME+" where "+T_User.USER_ID +" = ?";//用在查询用户信息
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,id);
            preparedStatement.executeUpdate();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try{
                connectSqlDao.closeConnection();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return false;
    }

    @Override
    public HashMap selectAll() {
        HashMap allUsers = new HashMap();
        Connection connection =null;
        try {
            connectSqlDao.initConnect();
            connection = connectSqlDao.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }

        Statement statement = null;
        try {
            statement = connection.createStatement();
            String sql = "select * from "+ T_User.TABLE_NAME;//用在查询用户信息
            ResultSet resultSet = statement.executeQuery(sql);
            while(resultSet.next()){
                User user = new User();
                user.setUser_id(resultSet.getInt(T_User.USER_ID));
                user.setAuth_id(resultSet.getInt(T_User.AUTH_ID));
                user.setUser_name(resultSet.getString(T_User.USER_NAME));
                user.setUser_password(resultSet.getString(T_User.USER_PASSWORD));
                allUsers.put(user.getUser_id(),user);
            }
            return allUsers;
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try{
                connectSqlDao.closeConnection();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    public boolean updateById(User user, int id) {
        return false;
    }

    @Override
    public boolean insertUser(User user) {
        Connection connection =null;
        try {
            connectSqlDao.initConnect();
            connection = connectSqlDao.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
        PreparedStatement preparedStatement = null;
        try {
            String sql = " insert into t_user (auth_id,user_name,user_password) value (?,?,?) ";//用在添加用户信息
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,user.getAuth_id());
            preparedStatement.setString(2,user.getUser_name());
            preparedStatement.setString(3,user.getUser_password());
            preparedStatement.executeUpdate();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try{
                connectSqlDao.closeConnection();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return false;
    }
}
